$(function(){
    var obj ={
        map: null,
        site: $.getQueryString('siteName'),
        init: function () {
            this.initMap();
        },
        initMap:function () {
            var that = this;
            var site = decodeURI(that.site);
            map = new BMap.Map("container");          // 创建地图实例
            map.centerAndZoom(site||'绍兴', 13);
            map.enableScrollWheelZoom();
            var old = localStorage.getItem('point');
            var obj = JSON.parse(old || '{}') || {};
            if(((new Date()).getTime()-obj.bulidTime>3600000 || !obj.bulidTime)&&!site){
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r) {
                    if(this.getStatus() == BMAP_STATUS_SUCCESS){
                        var localpoint = {
                            bulidTime: (new Date()).getTime(),
                            point: r.point
                        }

                        localStorage.setItem('point',JSON.stringify(localpoint));
                        var point = new BMap.Point(r.point);
                        map.centerAndZoom(point, 15);
                        map.enableScrollWheelZoom(); // 允许滚轮缩放
                    }else {
                        map.centerAndZoom('绍兴', 15);
                        map.enableScrollWheelZoom(); // 允许滚轮缩放
                    }
                });
            }
            if(!that.isSupportCanvas()){
                $.alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~');
                return;
            }
            //热力点图
            that.ajaxPoints().then(function (res) {
                if(res.code == 0){
                    var points = res.data;
                    var html = "";
                    var ranks = points.slice(0,5);
                    console.log(ranks);
                    ranks.forEach(function(item,index){
                        html +="<li><span>"+(index+1)+"</span><p>"+item.areaName+"</p><b>"+item.count+"</b>培训点</li>"
                    })
                    $('#rank').html(html);
                    $('#rank').slideDown();
                    var heatmapOverlay = new BMapLib.HeatmapOverlay({
                        radius:20,
                        gradient: {
                            .2: 'rgb(184,222,106)',
                            .4: 'rgb(234,207,54)',
                            .6: 'rgb(255,172,102)',
                            .8: 'rgb(226,51,82)'
                        }
                    });
                    map.addOverlay(heatmapOverlay);
                    heatmapOverlay.setDataSet({data:points,max:10});
                }
            })
            // 区域图
            that.ajaxAreas().then(function(res){
                if(res.code == 0){
                    res.data.forEach(function(item){
                        var points = [];
                        item.area.forEach(function(item){
                            var point = new BMapGL.Point(item.lng,item.lat);
                            points.push(point);
                        })
                    
                        var polygon = new BMapGL.Polygon(points, {strokeColor:"#00f", strokeWeight:2, strokeOpacity:0.5});
                        console.log(polygon);
                        map.addOverlay(polygon);
                        console.log(map);
                    })
                }
            });
            document.getElementById('searchIpt').addEventListener('input',function () {
                var city = $(this).val();
                map.centerAndZoom(city,14);
                map.setCurrentCity(city);
                map.clearOverlays();
                var local = new BMap.LocalSearch('绍兴', {
                    renderOptions:{map: map,autoViewport: true,panel: "r-result"},
                    pageCapacity: 8
                });
                local.search('绍兴'+city);
            })
            $('#rankBtn').click(function(){
                $('#rank').slideToggle();
            })
        },
        ajaxPoints: function () {
            var option = {
                url: 'http://aaa/getPoints',
                type: 'post',
                contentType: 'application/json',
                success: function (res) {
                    //
                    // {
                    //     "code":0,
                    //     "data":[
                    //         {"lng":120.596634,"lat":29.996686,"count":50},
                    //         {"lng":120.596814,"lat":29.997828,"count":51},
                    //         {"lng":120.596104,"lat":30.039137,"count":15}
                    //     ]
                    // }
                },
                error: function (res) {

                }
            };
            return $.getData(option)
        },
        ajaxAreas: function () {
            var option = {
                url: 'http://aaa/getAreas',
                type: 'post',
                contentType: 'application/json',
                success: function (res) {
                    // {
                    //     "code":0,
                    //     "data":[
                    //         {"area":[[120.596634,29.996686],[120.596654,29.996666],[120.596655,29.996666]],"areaName":'名称'},
                    //     ]
                    // }
                },
                error: function (res) {

                }
            };
            return $.getData(option)
        },
        isSupportCanvas: function(){
            var elem = document.createElement('canvas');
            return !!(elem.getContext && elem.getContext('2d'));
        }
    }
    obj.init();
})